// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var validator = require("validate");
	var constant = require("constant");
	var tools = require("tools");
	var E = {};
	E.loadData = function() {
		$(".load-list").each(function(idx, itm) {
			E.loadList(itm);
		});
	}

	E.disableInput = function(dom) {
		$(dom).find("input,textarea,select").attr("readonly", "readonly");
		$(dom).find("input,textarea,select").attr("disabled", "disabled");
	}
	E.enableInput = function(dom) {
		$(dom).find("input,textarea,select").removeAttr("readonly");
		$(dom).find("input,textarea,select").removeAttr("disabled");
	}

	E.fillForm = function(dom, data) {
		var d = $(dom);
		$.each(d.find("input,textarea,select,.data"), function(idx, itm) {
			var it = $(itm);
			var val = undefined
			var path = it.attr("path");
			if (path) {
				ps = path.split(".");
				var obj = data;
				$.each(ps, function(idx, itm) {
					obj = obj[itm];
					if (!obj) {
						return false;
					}
				});
				val = obj;
			} else {
				val = data[it.attr("name")];
			}
			if (typeof(val) == "undefined") {
				return;
			}
			if (it.hasClass("data")) {
				if (it.attr("formatC") == "1") {
					it.html(tools.formatCurrency(val));
				} else {
					it.html(val);
				}
			} else {
				if (it.attr("type") == "radio" || it.attr("type") == "checkbox") {
					if (it.val() == val) {
						it.attr("checked", "checked");
					}
				} else{
					it.val(val.toString());
//					it.attr("v", val);
				}
			}
		});
	}


	E.fillInput1 = function(dom, data, callback, error) {
		var d = $(dom);
		var action = data.action
		var version = data.version
		var async = data.async
		var nocode = data.nocode
		var extInput = d.attr("ext-input");

		var innerForm = base.parseForm(dom);
		var extendForm = base.parseForm($(extInput));
		var oData = $.extend(innerForm, extendForm);

		base.fnAjax({
			appKey: base.getData("appKey"),
			action: action,
			version: version,
			async: false,
			nocode: nocode,
			oData: oData, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas.length > 0) {
						E.fillForm(d, data.datas[0]);
					}
					callback(data);
				} else {
					if (error == null) {
						base.alert(3, data.message);
					}
				}
			}
		});
	}
	
	E.fillInput = function(dom, data) {
		var d = $(dom);
		var action = data.action
		var version = data.version
		var async = data.async
		var nocode = data.nocode
		var extInput = d.attr("ext-input");

		var innerForm = base.parseForm(dom);
		var extendForm = base.parseForm($(extInput));
		var oData = $.extend(innerForm, extendForm);

		base.fnAjax({
			appKey: base.getData("appKey"),
			action: action,
			version: version,
			async: async,
			nocode: nocode,
			oData: oData, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas.length>0){
						E.fillForm(d, data.datas[0]);	
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	}

	E.submitDom = function(dom, callback) {
		var d = $(dom);
		var action = d.attr("action") + ".json";
		var version = d.attr("version");
		var async = d.attr("async");
		var nocode = d.attr("nocode");
		var extInput = d.attr("ext-input");

		var innerForm = base.parseForm(dom);
		var extendForm = base.parseForm($(extInput));
		var oData = $.extend(innerForm, extendForm);

		var promptDialog = undefined;

		promptDialog = validator.validateInput(dom);
		if (promptDialog.length != 0) {
			base.alert(3, promptDialog);
			return
		}
		promptDialog = validator.validateInput($(extInput));
		if (promptDialog.length != 0) {
			base.alert(3, promptDialog);
			return
		}

		base.fnAjax({
			appKey: base.getData("appKey"),
			action: action,
			version: version,
			async: async,
			nocode: nocode,
			oData: oData, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (callback) {
						callback(data);
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	}

	E.loadList = function(dom, callback) {
		var d = $(dom);
		var action = d.attr("action") + ".json";
		var version = d.attr("version");
		var async = d.attr("async");
		var nocode = d.attr("nocode");
		var tplId = d.attr("tpl-id");
		var extInput = d.attr("ext-input");

		var innerForm = base.parseForm(dom);
		var extendForm = base.parseForm($(extInput));
		var oData = $.extend(innerForm, extendForm);


		base.fnAjax({
			appKey: base.getData("appKey"),
			action: action,
			version: version,
			async: async,
			nocode: nocode,
			oData: oData, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (tplId) {
						var tpl = $("#" + tplId).html();
						juicer.register("dict", constant.getValueByKey);
						var html = juicer(tpl, data);
						d.find(".list-body").html(html);

					}
					var totalPageCount = data.totalPageCount;
					var totalRecordCount = data.totalRecordCount;
					base.pageControl(d.find("div.pagenation,#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							E.fillForm(dom, {
								pageIndex: p
							});
							E.loadList(dom, data);
						});
					if (callback) {
						callback(data);
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});


	}

	E.init = function() {
		E.loadData();
	}


	return E;


});